﻿using SBlogCore.Common.JWTExtend.Models;
using SBlogCore.WebAdminApplication.SysManager.Dto;
using SBlogCore.WebAdminApplication.SysRoleManager.Dto;
using SBlogCore.WebAdminApplication.SysRoleManager.IServices;

namespace SBlogCore.WebAdminApplication.SysRoleManager.Controllers
{
    /// <summary>
    /// 角色管理
    /// </summary>
    public class SysRoleManagerController : AuthorizeBaseController
    {
        private readonly ISysRoleManagerService _sysRoleManagerService;

        /// <summary>
        /// 
        /// </summary>
        public SysRoleManagerController(ISysRoleManagerService sysRoleManagerService)
        {
            this._sysRoleManagerService = sysRoleManagerService;
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="inPut"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<PageList<SysRoleInfoOutPut>> GetList([FromQuery]SysRoleInfoQueryInPut inPut)
        {
            var res = await _sysRoleManagerService.GetList(inPut);
            return res.Adapt<PageList<SysRoleInfoOutPut>>();
        }

        /// <summary>
        /// 获取Options数据
        /// </summary>
        /// <param name="inPut"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<PageList<SysRoleInfoOptions>> GetOptions([FromQuery]SysRoleInfoQueryInPut inPut)
        {
            var res = await _sysRoleManagerService.GetList(inPut);
            PageList<SysRoleInfoOptions> pageList = new PageList<SysRoleInfoOptions>()
            {
                PageSize = res.PageSize,
                PageIndex = res.PageIndex,
                Items = res.Items.Adapt<List<SysRoleInfoOptions>>(),
                TotalCount = res.TotalCount
            };
            return pageList;
        }

        /// <summary>
        /// 创建数据
        /// </summary>
        /// <param name="inPut"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task AddSysRole(SysRoleInfoInPut inPut)
        {
            await _sysRoleManagerService.Add(inPut);
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="inPut"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task UploadSysRole(SysRoleInfoUploadInPut inPut)
        {
            await _sysRoleManagerService.Upload(inPut);
        }

        /// <summary>
        /// 为角色添加用户
        /// </summary>
        /// <param name="inPut"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task UpdateRoleUser(RoleUserInPut inPut)
        {
            await _sysRoleManagerService.UpdateRoleUser(inPut);
        }

        /// <summary>
        /// 为角色移除用户
        /// </summary>
        /// <param name="inPut"></param>
        /// <returns></returns>
        [HttpPost]

        public async Task MoveRoleUser(RoleUserInPut inPut)
        {
            await _sysRoleManagerService.MoveRoleUser(inPut);
        }

        /// <summary>
        /// 为用户添加角色
        /// </summary>
        /// <param name="inPut"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task UpdateUserRole(UserRoleInPut inPut)
        {
            await _sysRoleManagerService.UpdateUserRole(inPut);
        }

        /// <summary>
        /// 为用户添加角色
        /// </summary>
        /// <param name="inPut"></param>
        /// <returns></returns>
        [HttpPost]

        public async Task MoveUserRole(UserRoleInPut inPut)
        {
            await _sysRoleManagerService.MoveUserRole(inPut);
        }

        /// <summary>
        /// 通过权限ID获取所有用户，并标识该用户是否拥有该权限
        /// </summary>
        /// <param name="inPut"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<PageList<UserWithRoleOutPut>> GetUserWithRole([FromQuery]UserInfoWithRoleQueryInPut inPut)
        {
            return await _sysRoleManagerService.GetUserInfoWithRole(inPut);
        }

        /// <summary>
        /// 通过用户ID获取所有权限，并标识该用户是否拥有某个权限
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns> 
        [HttpGet]
        public async Task<List<long>> GetRoleWithUser(long userId)
        {
            return await _sysRoleManagerService.GetRoleWithUser(userId);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task DeleteSysRole(List<long> ids)
        {
           await _sysRoleManagerService.Delete(ids);
        }
        /// <summary>
        /// 是否存在编码
        /// </summary>
        /// <param name="value"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<bool> AnyCode(string value, long? id = null)
        {
            return  await _sysRoleManagerService.AnyCode(value, id);
        }

        /// <summary>
        /// 获取拥有当前菜单的角色
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>
        [HttpGet]

        public async Task<List<long>> GetRoleWithMenu(long menuId)
        {
            return await _sysRoleManagerService.GetRoleWithMenu(menuId);
        }

        /// <summary>
        /// 为角色添加菜单
        /// </summary>
        /// <param name="inPut"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task UpdateRoleWithMenu(RoleMenuInPut inPut)
        {
            await _sysRoleManagerService.UpdateRoleWithMenu(inPut);
        }

        /// <summary>
        /// 移除角色所拥有的的菜单
        /// </summary>
        /// <param name="inPut"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task MoveRoleWithMenu(RoleMenuInPut inPut)
        {
            await _sysRoleManagerService.MoveRoleWithMenu(inPut);
        }

        /// <summary>
        /// 为菜单添加添加角色
        /// </summary>
        /// <param name="inPut"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task UpdateMenuWithRole(MenuRoleInPut inPut)
        {
            await _sysRoleManagerService.UpdateMenuWithRole(inPut);
        }

        /// <summary>
        /// 移除拥有该菜单的角色
        /// </summary>
        /// <param name="inPut"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task MoveMenuWithRole(MenuRoleInPut inPut)
        {
            await _sysRoleManagerService.MoveMenuWithRole(inPut);
        }

        /// <summary>
        /// 获取当前角色所拥有的菜单权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<long>> GetMenuWithRole(long roleId)
        {
            return await _sysRoleManagerService.GetMenuWithRole(roleId);
        }
        /// <summary>
        /// 获取当前登录用户所拥有的菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<SysMenuOutPut>> GetMenuWithUserRole()
        {
            TokenModelJwt user = GetTokenUser();
            var res = await _sysRoleManagerService.GetMenuWithUserRole(user);
            return res.Adapt<List<SysMenuOutPut>>();
        }
    }
}
